1446C - Xor Tree - CodeForces Solution


binary search bitmasks data structures divide and conquer dp trees *2100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

#define all(x) begin(x),end(x)

template<class T>
    using iset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

void solve(){
    int n;
    cin>>n;
    vector<long long> a(n);
    for(auto&i:a){
	cin>>i;
    }
    sort(all(a));
    
    auto rec=[&](auto&&rec,int begi,int endi,long long TARGET)
    {
	if(endi-begi<=1)return 0;
	
	int pos=begi-1;
	for(int jump=__lg(endi-1-begi);jump>=0;jump--){
	    int posjump=pos+(1<<jump);
	    if(posjump<endi&&((a[posjump]&TARGET)==0))pos=posjump;
	}
	pos++;

	int removeRight=max(0,endi-pos-1);
	int removeLeft=max(0,pos-begi-1);
	if(TARGET==0){
	    return min(removeLeft,removeRight);
	}
	
	int solLeft=rec(rec,begi,pos,TARGET>>1)+removeRight;
	int solRight=rec(rec,pos,endi,TARGET>>1)+removeLeft;
	return min(solLeft,solRight);
    };

    int sol=rec(rec,0,n,1ll<<32);
    cout<<sol<<'\n';
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int numberOfTests=1;
    //cin>>numberOfTests;
    while(numberOfTests--){
	solve();
    }
}


Comments

Submit
0 Comments
More Questions

1679B - Stone Age Problem
402A - Nuts
792A - New Bus Route
221A - Little Elephant and Function
492C - Vanya and Exams
1369B - AccurateLee
892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion
946A - Partition
1068B - LCM
1692E - Binary Deque
679A - Bear and Prime 100
488A - Giga Tower
14A - Letter
1150A - Stock Arbitraging
1552A - Subsequence Permutation
1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You